package controller

import (
	code2 "devops/code"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"message"`
	Data interface{} `json:"data"`
}

func SuccessResponse(c *gin.Context, data interface{}) {
	response(c, code2.SUCCESS, data)
}

func SuccessPaginateResponse(c *gin.Context, data interface{}, total int, curPage int, curPageSize int) {
	pageResponse := gin.H{
		"cur_page": curPage,
		"cur_page_size": curPageSize,
		"total": total,
		"total_page": total/curPageSize,
		"data": data,
	}
	response(c, code2.SUCCESS, pageResponse)
}

func ErrorResponse(c *gin.Context, errCode code2.Err, data interface{}) {
	response(c, errCode, data)
}

func response(c *gin.Context, code code2.Err, data interface{}) {
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"message": code2.ErrMsg[code],
		"data": data,
	})
}

func NoRouter(c *gin.Context) {
	ErrorResponse(c, code2.NOTFOUND, nil)
}

func Hello(c *gin.Context) {
	SuccessResponse(c, map[string]interface{}{
		"time": time.Now(),
		"version": "v0.0.1",
	})
}
